package com.itrip.trade.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itrip.beans.pojo.HotelOrder;
import com.itrip.beans.pojo.TradeEnds;
import com.itrip.dao.HotelOrderMapper;
import com.itrip.dao.TradeEndsMapper;
import com.itrip.trade.service.OrderService;
import com.itrip.utils.util.EmptyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: 周勉
 * @date: 2020/3/13 18:15
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private HotelOrderMapper hotelOrderMapper;
    @Resource
    private TradeEndsMapper tradeEndsMapper;
    @Override
    public HotelOrder loadItripHotelOrder(String orderNo) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("order_no",orderNo);
        return hotelOrderMapper.selectOne(qw);
    }

    @Override
    public boolean processed(String orderNo) {
        HotelOrder itripHotelOrder=this.loadItripHotelOrder(orderNo);
        return itripHotelOrder.getOrderStatus().equals(2)&&
                !EmptyUtils.isEmpty(itripHotelOrder.getTradeNo());
    }

    @Override
    public void paySuccess(String orderNo, int payType, String tradeNo) {
        HotelOrder itripHotelOrder=this.loadItripHotelOrder(orderNo);
        itripHotelOrder.setOrderStatus(2);//支付成功
        itripHotelOrder.setPayType(payType);
        itripHotelOrder.setTradeNo(tradeNo);//交易号（如支付宝交易号）
        hotelOrderMapper.updateById(itripHotelOrder);

        //增加订单后续待处理记录
        TradeEnds itripTradeEnds=new TradeEnds();
        itripTradeEnds.setId(itripHotelOrder.getId());
        itripTradeEnds.setOrderNo(itripHotelOrder.getOrderNo());
        tradeEndsMapper.insert(itripTradeEnds);

    }

    @Override
    public void payFailed(String orderNo, int payType, String tradeNo) {
        HotelOrder itripHotelOrder=this.loadItripHotelOrder(orderNo);
        itripHotelOrder.setOrderStatus(1);//支付状态：已取消
        itripHotelOrder.setPayType(payType);
        itripHotelOrder.setTradeNo(tradeNo);//交易号（如支付宝交易号）
        hotelOrderMapper.updateById(itripHotelOrder);
    }
}
